package com.cy.mall.service.order.impl;

import com.cy.mall.comm.utils.CommonUtils;
import com.cy.mall.dao.mapper.order.OrderMapper;
import com.cy.mall.model.dto.order.RepaymentRemindDTO;
import com.cy.mall.model.enums.MessageType;
import com.cy.mall.service.BaseService;
import com.cy.mall.service.message.MessageService;
import com.cy.mall.service.order.OrderJobService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @Description 还款提醒 service 实现
 * @Author tanyilin
 * @Date 2019/8/12 16:29
 */
@Slf4j
@Service
public class OrderJobServiceImpl extends BaseService implements OrderJobService {

    @Autowired
    private MessageService messageService;
    @Autowired
    private OrderMapper    orderMapper;


    @Override
    public void doRepaymentRemind() {

        //查询处于'还款中'的订单信息
        List<RepaymentRemindDTO> list = orderMapper.selectRepaymentRemindDTO();
        if(CommonUtils.isEmpty(list)) {
            log.info("定时任务--需要还款提醒的订单数据有 [0] 条");
            return;
        }
        //循环进行还款提醒
        log.info("定时任务--需要还款提醒的订单数据有 [{}] 条", list.size());
        for(RepaymentRemindDTO remindDTO : list) {
            //如果当天需要发送提醒消息
            if(CommonUtils.isEmpty(remindDTO.getLoanTime())) {
                log.info("放款时间异常，loanId=[{}]", remindDTO.getThirdLoanId());
                continue;
            }
            if(isSendMessage(remindDTO.getLoanTime(), new Date())) {
                messageService.sendMessage(remindDTO.getThirdLoanId(), MessageType.REPAYMENT_REMIND);
                //log.info("发送还款提醒信息，loanId=[{}]", remindDTO.getThirdLoanId());
            }else {
                log.info("今天不需要发送还款提醒消息，loanId=[{}]", remindDTO.getThirdLoanId());
            }
        }
    }

    /**
     * 判断今天是否需要发送还款提醒消息
     * @param loanTime
     * @param nowTime
     * @return boolean
     */
    private boolean isSendMessage(Date loanTime, Date nowTime) {

        //计算当前月的还款日
        Calendar begin = Calendar.getInstance();
        begin.setTime(loanTime);
        int day = begin.get(Calendar.DAY_OF_MONTH);

        //获取当前月的最大天数
        Calendar now = Calendar.getInstance();
        now.setTime(nowTime);
        int maxNowDay = now.getActualMaximum(Calendar.DAY_OF_MONTH);
        //判断
        int hkDay = 0;
        //如果还款日 大于当前月的最大天数,  那么还款日 就是当前的最大天数
        if(day > maxNowDay) {
            hkDay = maxNowDay;
            begin.set(Calendar.DAY_OF_MONTH, hkDay);
        }else {
            //否则，还款日就是当前月的还款日
            hkDay = day;
        }
        log.info("当前月的还款日：{}日", hkDay);

        int nowDay = now.get(Calendar.DAY_OF_MONTH);

        //如果当天=还款日
        if(nowDay == hkDay) {
            return true;
        }

        //计算提醒日，规则：还款日前3天、前5天
        begin.add(Calendar.DAY_OF_MONTH, -3);
        int txDay = begin.get(Calendar.DAY_OF_MONTH);
        if(nowDay == txDay) {
            return true;
        }

        begin.add(Calendar.DAY_OF_MONTH, -2);
        txDay = begin.get(Calendar.DAY_OF_MONTH);
        if(nowDay == txDay) {
            return true;
        }

        return false;
    }
}
